<?php
/*------------------------------------------------------------------------
 # SM Deal - Version 1.0
 # Copyright (c) 2014 YouTech Company. All Rights Reserved.
 # @license - Copyrighted Commercial Software
 # Author: YouTech Company
 # Websites: http://www.magentech.com
-------------------------------------------------------------------------*/?>
		
<script type="text/javascript">
//<![CDATA[
data = new Date(2013,10,26,12,00,00);
var listdeal = [];
function CountDown(date,id){
	dateNow = new Date();
	amount = date.getTime() - dateNow.getTime();
	delete dateNow;
	if(amount < 0){
		document.getElementById(id).innerHTML="Now!";
	} else{
		days=0;hours=0;mins=0;secs=0;out="";
		amount = Math.floor(amount/1000);
		days=Math.floor(amount/86400);
		amount=amount%86400;
		hours=Math.floor(amount/3600);
		amount=amount%3600;
		mins=Math.floor(amount/60);
		amount=amount%60;
		secs=Math.floor(amount);
		if(days != 0){out += "<div class='time-item time-day'>" + "<div class='num-time'>" + days + "</div>" +" <div class='name-time'>"+((days==1)?"Day":"Days") + "</div>"+"</div> ";}
		if(hours != 0){out += "<div class='time-item time-hour'>" + "<div class='num-time'>" + hours + "</div>" +" <div class='name-time'>"+((hours==1)?"Hour":"Hours") + "</div>"+"</div> ";}
		out += "<div class='time-item time-min'>" + "<div class='num-time'>" + mins + "</div>" +" <div class='name-time'>"+((mins==1)?"Min":"Mins") + "</div>"+"</div> ";
		out += "<div class='time-item time-sec'>" + "<div class='num-time'>" + secs + "</div>" +" <div class='name-time'>"+((secs==1)?"Sec":"Secs") + "</div>"+"</div> ";
		out = out.substr(0,out.length-2);
		document.getElementById(id).innerHTML=out;
		setTimeout(function(){CountDown(date,id)}, 1000);
	}
}
//]]>
</script>
<?php
$dayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$deals  = Mage::getResourceModel('deal/deal_product_collection')->joinFields()->joinFieldsdeal()
				->addFilter('status', '1', "=")
				->addFilter('end_date', $dayDate, ">");

if(Mage::getStoreConfig('deal/deal/orderby') == 1){
	$deals->OrderbyAdd('start_date','desc');
}else{
	$deals->OrderbyAdd('end_date','desc');
}
		
$productBlock=$this->getLayout()->createBlock("catalog/product_list");
$check_array =$deals->getData();

/*get config backend*/
$general_setting = Mage::getStoreConfig('deal/deal');
$product_setting = Mage::getStoreConfig('deal/product_display_setting');
?>
<?php if( !empty($check_array) && $general_setting['isenabled'] == 1 ){
	$uniqued ='sm_deal_'.rand().time();
?>
<div id="<?php echo $uniqued;?>" class="sm-deal-wrap">
	<div class="w-title">
		<h2><?php echo $this->__('Deals of the day');?></h2>
		<ul class="control-button">
			<li class="prev_deal"><i></i>Prev</li>
			<li class="next_deal"><i></i>Next</li>
		</ul>
	</div>

	<div class="container-slider">
		<div class="sm-deal-inner">
			<div id="box-dailydeal" class="slider cols-3  preset01-3 preset02-2 preset03-2 preset04-1 preset05-1 js-loaded">
				<div class="deal-list-item">
				<div class="vpo-wrap">
				<div class="vp">
				<div class="vpi-wrap">
					<?php $j = 0; foreach($deals->getData() as $product_data){ 
					$_product = Mage::getModel('catalog/product')->load($product_data['entity_id']); 
					
					$now = date("Y-m-d H:m:s");
					$newsFrom= $_product->getNewsFromDate(); //substr($_product->getData('news_from_date'),0,10);
					$newsTo= $_product->getNewsToDate();   //substr($_product->getData('news_to_date'),0,10);
					$specialprice = Mage::getModel('catalog/product')->load($_product->getId())->getSpecialPrice();
					$price = Mage::getModel('catalog/product')->load($_product->getId())->getPrice();
					$special_from_date = $_product->getSpecialFromDate();
					$special_to_date = $_product->getSpecialToDate();
					?>
					
					
					<div class="item-deal item respl-item">
						<div class="item-inner">
							<div class="w-image-box">
								<div class="item-image">
									<a  href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">											
										<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize($product_setting['product_image_width'],$product_setting['product_image_height']); ?>"/>
									</a>
									
									<a title="<?php echo $this->__('View Details');?>" href="<?php echo $_product->getProductUrl() ?>" class="view-detail">
										<?php echo $this->__('View Details');?>
									</a>
								</div>
								<?php if ($newsFrom !="" && $now>=$newsFrom && ($now<=$newsTo || $newsTo=="")){?>
									<div class="new-product">
										<div class="new-content">
											<?php echo $this->__('New'); ?>
										</div>
									</div>
								<?php }?>
								
								<?php if (($specialprice && ($special_from_date <= $now) && ($special_to_date >= $now || $special_to_date =="" ))){?>
									<div class='sale-item'>
										<?php echo $this->__('Save'); ?>
										<span class="n-sale">
											<?php
												$saleof= abs(($specialprice/($_product->getPrice()))*100-100);
												print_r(floor($saleof));
											?>%
										</span>
										
									</div>
								<?php }?>
								
								<div class="item-time">							
									<div class="item-timer" id="product_time_<?php echo $j;?>"></div>	
									<script type="text/javascript">
									//<![CDATA[
										listdeal.push('product_time_<?php echo $j."&&||&&".date("Y/m/d", strtotime($product_data['end_date']));?>') ;
									//]]>
									</script>											
								</div>
							</div>
							
							<div class="item-info">
								<?php if( $product_setting['product_title_disp'] == 1 ){?>
								<div class="item-title">
									<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">
									<?php echo substr($this->htmlEscape($_product->getName()), 0, $product_setting['product_title_max_characters']);
									//echo $product_setting['product_title_max_characters'];
									?>
									</a>
								</div>
								<?php }?>
								
								<?php if( $product_setting['product_reviews_count'] == 1 ){?>
								<div class="item-review">
									<?php  echo $productBlock->getReviewsSummaryHtml($_product, false, true); ?>
								</div>
								<?php }?>
								
								<?php if( $product_setting['product_price_disp'] == 1 ){?>
								<div class="item-price">
									<?php if($_product->isSaleable()){
										echo $productBlock->getPriceHtml($_product, true); 
									} ?>
								</div>	
								<?php }?>
								
								<?php if( $product_setting['product_description_disp'] == 1 ){?>
								
									<?php// echo $_product->getshort_description();?>
									<?php 
										$max_leng = $product_setting['product_description_max_characters'];
										echo substr($_product->getShortDescription(), 0, $max_leng);
									?>
								
								<?php }?>
								<!--
								<div class="item-addto-wrap">
									<?php if($_product->getIsSalable()): ?>
										<button class="button btn-cart" onclick="setLocation('<?php echo Mage::helper('checkout/cart')->getAddUrl($_product) ?>')"><span><?php echo $this->__('Add to Cart') ?></span></button>
										<?php else: ?>
										<div class="out-of-stock"><?php echo $this->__('Out of stock') ?></div>
									<?php endif; ?>
								
									<?php if( $_compareUrl= $productBlock->getAddToCompareUrl($_product) ): ?>
										<a class="link-compare" href="<?php echo $_compareUrl ?>" title="<?php echo $this->__('Add to Compare'); ?>">
											<?php echo $this->__('Add to Compare') ?>
										</a>
									<?php endif;?>
									
									
									<?php if ( $this->helper('wishlist')->isAllow() ) : ?>
									<a class="link-wishlist" href="<?php echo Mage::helper('wishlist')->getAddUrl($_product) ?>" title="<?php echo $this->__('Add to Wishlist') ?>">
										<?php echo $this->__('Add to Wishlist') ?>
									</a>
									<?php endif; ?>
								
								</div>
								-->
							</div>
						</div>
					</div>
					<?php $j++; }?>			
				</div>
				</div>
				</div>
				</div>
			</div>
		</div>
	</div>
</div>
<script>
	$jsmart(document).ready(function($){
		var w_window = $('body').width();
		var step_s = 1;
		if(w_window >= 1200){
			step_s = 3;
		}
		if(w_window >= 992 && w_window <= 1190){
			step_s = 2;
		}
		if(w_window >= 480 && w_window <= 991){
			step_s = 2;
		}
		if(w_window <= 479){
			step_s = 1;
		}
		
        $('#box-dailydeal').responsiver({
            interval: 0,
			speed: 300,
			start: 0,
			step: step_s,
			circular: false,
			preload: true,
			fx: 'slide',
			pause: 'hover',
			control:{
			prev: '.control-button li[class="prev_deal"]',
			next: '.control-button li[class="next_deal"]'
			},
			getColumns: function(element){
			var match = $(element).attr('class').match(/cols-(\d+)/);
			if (match[1]){
			var column = parseInt(match[1]);
			} else {
			var column = 1;
			}
			if (!column) column = 1;
			return column;
			}
        });
    });
</script>
<script type="text/javascript">
//<![CDATA[
window.onload=function(){
	if(listdeal.length > 0){
		for(i=0;i<listdeal.length;i++)
		{
			var arr = listdeal[i].split("&&||&&"); 
			var data = new Date(arr[1]);
			CountDown(data, arr[0]);
		}	
	}
};
//]]>
</script>	
<?php } ?>

